include .env

PROXY_ADDRESS?=http://192.168.103.31:7890
OPERATOR_NAMESPACE?=clickhouse
# USER_NAME?=clickhouse
# USER_PASSWORD?=$(shell kubectl get secret ${USER_NAME} -n ${OPERATOR_NAMESPACE} -o jsonpath='{.data.password}' | base64 -d )

jdbc_image_name?=uco-clickhouse-jdbc-bridge
jdbc_image_version?=0.2.1
export

.PHONY: build install_test delete_test install_prod delete_prod build_jdbc_bridge jdbc_config test_jdbc_bridge install_jdbc_bridge teardown_jdbc_bridge install_ingress secret

# detail file
get_operator_template:
	wget -O ./clickhouse-operator-install-template.yaml https://raw.githubusercontent.com/Altinity/clickhouse-operator/master/deploy/operator/clickhouse-operator-install-template.yaml

get_operator_shell:
	wget -O ./clickhouse-operator-install.sh https://raw.githubusercontent.com/Altinity/clickhouse-operator/master/deploy/operator-web-installer/clickhouse-operator-install.sh
	wget -O ./clickhouse-operator-delete.sh https://raw.githubusercontent.com/Altinity/clickhouse-operator/master/deploy/operator-web-installer/clickhouse-operator-delete.sh

# (Important)
ctr_image_update:
	sed -i 's/altinity\/clickhouse-operator:/docker.io\/altinity\/clickhouse-operator:/g' ./clickhouse-operator-install.sh
	sed -i 's/altinity\/clickhouse-operator:/docker.io\/altinity\/clickhouse-operator:/g' ./clickhouse-operator-delete.sh
	sed -i 's/altinity\/metrics-exporter:/docker.io\/altinity\/metrics-exporter:/g' ./clickhouse-operator-install.sh

# execute install clickhouse operator shell by env, also can upgrade
install_chop:
	HTTP_PROXY=${PROXY_ADDRESS} HTTPS_PROXY=${PROXY_ADDRESS} bash clickhouse-operator-install.sh

teardown_chop:
	@echo ${OPERATOR_NAMESPACE}
	bash clickhouse-operator-delete.sh

build:
	rm -rf build
	mkdir -p build/jdbc-bridge
	envsubst < clickhouse-test.yaml > ./build/clickhouse-test.yaml
	envsubst < clickhouse-prod.yaml > ./build/clickhouse-prod.yaml
	for name in jdbc-bridge/ds*.json; do\
		ESCAPE=$$ envsubst < $${name} > ./build/$${name}; \
    done

install_test: build
	kubectl apply -f ./build/clickhouse-test.yaml -n ${OPERATOR_NAMESPACE}
delete_test:
	kubectl delete -f ./build/clickhouse-test.yaml -n ${OPERATOR_NAMESPACE}

install_prod: build
	kubectl apply -f ./build/clickhouse-prod.yaml -n ${OPERATOR_NAMESPACE}

delete_prod:
	kubectl delete -f ./build/clickhouse-prod.yaml -n ${OPERATOR_NAMESPACE}

build_jdbc_bridge:
	docker build -t ${jdbc_image_name}:${jdbc_image_version} \
		./jdbc-bridge 
	docker image tag ${jdbc_image_name}:${jdbc_image_version} gitlab-registry.f.uco.com/p/it/big/uco-clickhouse-cluster/${jdbc_image_name}:${jdbc_image_version}
	docker image push gitlab-registry.f.uco.com/p/it/big/uco-clickhouse-cluster/${jdbc_image_name}:${jdbc_image_version}

jdbc_config: build
	files=" "; \
	for name in build/jdbc-bridge/ds*.json; do\
		file=$$(echo $$name | awk '{print $$NF}' FS="/"); \
		arg=$$(echo "--from-file=$$file=$$name"); \
		files="$$files $$arg"; \
    done;\
	kubectl create secret generic \
		uco-clickhouse-jdbc-bridge-datasources \
		$$files \
		-n ${OPERATOR_NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -

test_jdbc_bridge:
	helm template -f jdbc-bridge/ch-bridge.yaml ch-bridge jdbc-bridge/chart -n ${OPERATOR_NAMESPACE} > build/jdbc_bridge.yaml

install_jdbc_bridge:
	helm upgrade --install -f jdbc-bridge/ch-bridge.yaml ch-bridge jdbc-bridge/chart -n ${OPERATOR_NAMESPACE} \
	--set image.tag='${jdbc_image_version}'

teardown_jdbc_bridge:
	helm uninstall ch-bridge -n ${OPERATOR_NAMESPACE}

install_ingress:
	kubectl apply -f clickhouse-ingress.yaml -n ${OPERATOR_NAMESPACE}

delete_ingress:
	kubectl delete -f clickhouse-ingress.yaml -n ${OPERATOR_NAMESPACE}

secret:
	-kubectl delete secret obs-datalake-backup -n ${OPERATOR_NAMESPACE}
	-kubectl create secret generic obs-datalake-backup \
    	--from-literal=AWS_ACCESS_KEY_ID="${OBS_DATALAKE_AK}" \
    	--from-literal=AWS_SECRET_ACCESS_KEY="${OBS_DATALAKE_SK}" \
		--from-literal=AWS_ENDPOINTS="https://obs.cn-east-3.myhuaweicloud.com" \
		--from-literal=VIRTUAL_HOSTED_STYLE="true" \
		-n ${OPERATOR_NAMESPACE}

test_case: build
	-kubectl delete secret test-uco-clickhouse-jdbc-bridge-datasources -n ${OPERATOR_NAMESPACE}
	files=" "; \
	for name in build/jdbc-bridge/ds*.json; do\
		file=$$(echo $$name | awk '{print $$NF}' FS="/"); \
		arg=$$(echo "--from-file=$$file=$$name"); \
		files="$$files $$arg"; \
    done;\
	kubectl create secret generic \
		test-uco-clickhouse-jdbc-bridge-datasources \
		$$files \
		-n ${OPERATOR_NAMESPACE}
install_test_jdbc:
	helm upgrade --install -f jdbc-bridge/test-ch-bridge.yaml test-ch-bridge jdbc-bridge/chart -n ${OPERATOR_NAMESPACE} \
	--set image.tag='${jdbc_image_version}'

teardown_test_jdbc:
	helm uninstall test-ch-bridge -n ${OPERATOR_NAMESPACE}
